package cn.px.hro.insucance.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.px.common.annotation.DataScope;
import cn.px.common.exception.ServiceException;
import cn.px.common.utils.StringUtils;
import cn.px.common.utils.ThreadPoolUtil;
import cn.px.hro.common.enums.HroEnum;
import cn.px.hro.employee.domain.HroEmployee;
import cn.px.hro.employee.service.IHroEmployeeService;
import cn.px.hro.employee_project.domain.HroEmployeeProject;
import cn.px.hro.employee_project.domain.HroEmployeeProjectExportVo;
import cn.px.hro.employee_project.domain.HroEmployeeProjectVo;
import cn.px.hro.employee_project.domain.MakePaymentOrderVo;
import cn.px.hro.employee_project.service.IHroEmployeeProjectService;
import cn.px.hro.payment_form.domain.HroPaymentForm;
import cn.px.hro.payment_form.service.IHroPaymentFormService;
import cn.px.hro.payment_form_detail.domain.HroPaymentFormDetail;
import cn.px.hro.payment_form_detail.service.IHroPaymentFormDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.px.common.annotation.Log;
import cn.px.common.core.controller.BaseController;
import cn.px.common.core.domain.AjaxResult;
import cn.px.common.enums.BusinessType;
import cn.px.hro.insucance.domain.HroInsucance;
import cn.px.hro.insucance.service.IHroInsucanceService;
import cn.px.common.utils.poi.ExcelUtil;
import cn.px.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 保单Controller
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@RestController
@RequestMapping("/insucance/insucance")
public class HroInsucanceController extends BaseController {
    @Autowired
    private IHroInsucanceService hroInsucanceService;
    @Autowired
    private IHroEmployeeProjectService hroEmployeeProjectService;
    @Autowired
    private IHroEmployeeService employeeService;
    @Resource
    private IHroEmployeeProjectService employeeProjectService;
    @Resource
    private IHroPaymentFormService paymentFormService;
    @Resource
    private IHroPaymentFormDetailService paymentFormDetailService;

    /**
     * 查询保单列表
     */
    //@PreAuthorize("@ss.hasPermi('insucance:insucance:list')")
    @GetMapping("/list")
    @DataScope(deptAlias = "g", userAlias = "u")
    public TableDataInfo list(HroInsucance hroInsucance) {
        startPage();
        List<HroInsucance> list = hroInsucanceService.selectHroInsucanceList(hroInsucance);
        if (CollectionUtils.isNotEmpty(list)) {
            if (StringUtils.isNotBlank(hroInsucance.getAuditStatus()) && StringUtils.isNotBlank(hroInsucance.getStatus()) && StringUtils.isBlank(hroInsucance.getEffectStatus())) {
                if (hroInsucance.getAuditStatus().equals("0")) {
                    //待审核
                    for (HroInsucance item : list) {
                        // 待审核人数和人次
                        Map<String, Object> parm = new HashMap<>();
                        parm.put("auditStatus", "0");
                        parm.put("status", "0");
                        parm.put("companyId", item.getCompanyId());
                        parm.put("property", item.getProperty());
                        parm.put("insuranceStart", item.getInsuranceStart());
                        parm.put("agreementId", item.getAgreementId());
                        parm.put("insucanceId", item.getId());
                        HroEmployeeProjectVo hroEmployeeProjectVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
                        if (hroEmployeeProjectVo != null) {
                            if (hroEmployeeProjectVo.getTotalPeopleNum() != null) {
                                item.setTotalPeopleNum(hroEmployeeProjectVo.getTotalPeopleNum());
                            }
                            if (hroEmployeeProjectVo.getTotalPeopleTime() != null) {
                                item.setTotalPeopleTime(hroEmployeeProjectVo.getTotalPeopleTime());
                            }
                        }
                        //查询未匹配的人数
                        HroEmployee hroEmployee = new HroEmployee();
                        hroEmployee.setStatus("0");
                        hroEmployee.setInsucanceId(item.getId());
                        List<HroEmployee> employeesList = employeeService.selectHroEmployeeList(hroEmployee);
                        if (CollectionUtils.isNotEmpty(employeesList)) {
                            //加入到未审核人数
                            item.setNotMatchPeopleNum((long) employeesList.size());
                        } else {
                            item.setNotMatchPeopleNum(0L);
                        }


                        //已审核人数和人次
                        parm.clear();
                        parm.put("auditStatus", "1");
                        parm.put("status", "0");
                        parm.put("companyId", item.getCompanyId());
                        parm.put("property", item.getProperty());
                        parm.put("insuranceStart", item.getInsuranceStart());
                        parm.put("agreementId", item.getAgreementId());
                        parm.put("insucanceId", item.getId());
                        HroEmployeeProjectVo hroEmployeeProjectAlreadVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
                        if (hroEmployeeProjectAlreadVo != null) {
                            if (hroEmployeeProjectAlreadVo.getTotalPeopleNum() != null) {
                                item.setAlreayTotalPeopleNum(hroEmployeeProjectAlreadVo.getTotalPeopleNum());
                            }
                            if (hroEmployeeProjectAlreadVo.getTotalPeopleTime() != null) {
                                item.setAlreayTotalPeopleTime(hroEmployeeProjectAlreadVo.getTotalPeopleTime());
                            }
                        }

                        //总人数
                        parm.clear();
                        parm.put("insucanceId",item.getId());
                        HroEmployeeProjectVo totalCountPeopleNum = employeeService.selectPeopleNum(parm);
                        item.setTotalCountPeopleNum(totalCountPeopleNum.getTotalPeopleNum());


//                //已实际投保的人数和人次
//                parm.clear();
//                parm.put("status","3");
//                parm.put("companyId",item.getCompanyId());
//                parm.put("property",item.getProperty());
//                parm.put("insuranceStart",item.getInsuranceStart());
//                parm.put("agreementId",item.getAgreementId());
//                HroEmployeeProjectVo hroEmployeeProjectVoByTouBao = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
//                if(hroEmployeeProjectVoByTouBao != null){
//                    if(hroEmployeeProjectVoByTouBao.getTotalPeopleNum() != null){
//                        item.setTouBaoTotalPeopleNum(hroEmployeeProjectVoByTouBao.getTotalPeopleNum());
//                    }
//                    if(hroEmployeeProjectVoByTouBao.getTotalPeopleTime() != null){
//                        item.setTouBaoTotalPeopleTime(hroEmployeeProjectVoByTouBao.getTotalPeopleTime());
//                    }
//                }
                        //审核状态，如果此单子有未审核的或者有未匹配的都算是审核中，只有所有的人审核完成了，才能是已完成
                        //默认是已全部审核
                        String auditStatus = "1";
                        //没有被审核的人
                        HroEmployee employee = new HroEmployee();
                        employee.setInsuranceStart(item.getInsuranceStart());
                        employee.setProperty(item.getProperty());
                        employee.setCompanyId(item.getCompanyId());
                        employee.setAuditStatus("0");
                        employee.setInsucanceId(item.getId());
                        employee.setAgreementId(item.getAgreementId());
                        List<HroEmployee> employees = employeeService.selectHroEmployeeList(employee);
                        if (CollectionUtils.isNotEmpty(employees)) {
                            auditStatus = "0";
                        }
                        item.setFlag(auditStatus);
                    }
                } else if (hroInsucance.getAuditStatus().equals("1") && hroInsucance.getStatus().equals("3")) {
                    //已实际投保
                    for (HroInsucance item : list) {
                        // 待投保人数和人次
                        Map<String, Object> parm = new HashMap<>();
//                parm.put("auditStatus","1");
//                parm.put("status","0");
//                parm.put("companyId",item.getCompanyId());
//                parm.put("property",item.getProperty());
//                parm.put("insuranceStart",item.getInsuranceStart());
//                parm.put("agreementId",item.getAgreementId());
//                HroEmployeeProjectVo hroEmployeeProjectVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
//                if(hroEmployeeProjectVo != null){
//                    if(hroEmployeeProjectVo.getTotalPeopleNum() != null){
//                        item.setTotalPeopleNum(hroEmployeeProjectVo.getTotalPeopleNum());
//                    }
//                    if(hroEmployeeProjectVo.getTotalPeopleTime() != null){
//                        item.setTotalPeopleTime(hroEmployeeProjectVo.getTotalPeopleTime());
//                    }
//                }
                        //已投保人数和人次
                        parm.clear();
                        parm.put("auditStatus", "1");
                        parm.put("status", "3");
                        parm.put("companyId", item.getCompanyId());
                        parm.put("property", item.getProperty());
                        parm.put("insuranceStart", item.getInsuranceStart());
                        parm.put("agreementId", item.getAgreementId());
                        parm.put("insucanceId", item.getId());
                        HroEmployeeProjectVo hroEmployeeProjectAlreadVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
                        if (hroEmployeeProjectAlreadVo != null) {
                            if (hroEmployeeProjectAlreadVo.getTotalPeopleNum() != null) {
                                item.setAlreayTotalPeopleNum(hroEmployeeProjectAlreadVo.getTotalPeopleNum());
                            }
                            if (hroEmployeeProjectAlreadVo.getTotalPeopleTime() != null) {
                                item.setAlreayTotalPeopleTime(hroEmployeeProjectAlreadVo.getTotalPeopleTime());
                            }
                        }
//                //已实际投保的人数和人次
//                parm.clear();
//                parm.put("status","3");
//                parm.put("companyId",item.getCompanyId());
//                parm.put("property",item.getProperty());
//                parm.put("insuranceStart",item.getInsuranceStart());
//                parm.put("agreementId",item.getAgreementId());
//                HroEmployeeProjectVo hroEmployeeProjectVoByTouBao = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
//                if(hroEmployeeProjectVoByTouBao != null){
//                    if(hroEmployeeProjectVoByTouBao.getTotalPeopleNum() != null){
//                        item.setTouBaoTotalPeopleNum(hroEmployeeProjectVoByTouBao.getTotalPeopleNum());
//                    }
//                    if(hroEmployeeProjectVoByTouBao.getTotalPeopleTime() != null){
//                        item.setTouBaoTotalPeopleTime(hroEmployeeProjectVoByTouBao.getTotalPeopleTime());
//                    }
//                }
//                        //默认是没有全部投保
//                        String status = "1";
//                        //查询所有被审核通过人
//                        HroEmployee employee = new HroEmployee();
//                        employee.setInsuranceStart(item.getInsuranceStart());
//                        employee.setProperty(item.getProperty());
//                        employee.setCompanyId(item.getCompanyId());
//                        employee.setAuditStatus("1");
////                        employee.setAgreementId(item.getAgreementId());
//                        List<HroEmployee> employeeList = employeeService.selectHroEmployeeList(employee);
//                        if(CollectionUtils.isNotEmpty(employeeList)){
//                            for(HroEmployee itemEmp : employeeList){
//                                //根据员工id查询投保信息
//                                HroEmployeeProject queryEmployeePro = new HroEmployeeProject();
//                                queryEmployeePro.setEmployeeId(itemEmp.getId());
//                                queryEmployeePro.setStatus("0");
//                                queryEmployeePro.setAuditStatus("1");
//                                List<HroEmployeeProject> projectList = hroEmployeeProjectService.selectHroEmployeeProjectList(queryEmployeePro);
//                                if(CollectionUtils.isNotEmpty(projectList)){
//                                    status = "0";
//                                }
//                            }
//                        }
//                        item.setFlag(status);
                    }
                }
            } else if (StringUtils.isBlank(hroInsucance.getEffectStatus()) && hroInsucance.getAuditStatus().equals("1") && (hroInsucance.getStatusList() != null && hroInsucance.getStatusList().length > 0)) {
                //代投保
                for (HroInsucance item : list) {
                    // 待投保人数和人次
                    Map<String, Object> parm = new HashMap<>();
                    parm.put("auditStatus", "1");
                    parm.put("status", "0");
//                        parm.put("companyId",item.getCompanyId());
//                        parm.put("property",item.getProperty());
//                        parm.put("insuranceStart",item.getInsuranceStart());
//                        parm.put("agreementId",item.getAgreementId());
                    parm.put("insucanceId", item.getId());
                    HroEmployeeProjectVo hroEmployeeProjectVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
                    if (hroEmployeeProjectVo != null) {
                        if (hroEmployeeProjectVo.getTotalPeopleNum() != null) {
                            item.setTotalPeopleNum(hroEmployeeProjectVo.getTotalPeopleNum());
                        }
                        if (hroEmployeeProjectVo.getTotalPeopleTime() != null) {
                            item.setTotalPeopleTime(hroEmployeeProjectVo.getTotalPeopleTime());
                        }
                    }
                    //已投保人数和人次
                    parm.clear();
                    parm.put("auditStatus", "1");
                    parm.put("status", "3");
//                        parm.put("companyId",item.getCompanyId());
//                        parm.put("property",item.getProperty());
//                        parm.put("insuranceStart",item.getInsuranceStart());
//                        parm.put("agreementId",item.getAgreementId());
                    parm.put("insucanceId", item.getId());
                    HroEmployeeProjectVo hroEmployeeProjectAlreadVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
                    if (hroEmployeeProjectAlreadVo != null) {
                        if (hroEmployeeProjectAlreadVo.getTotalPeopleNum() != null) {
                            item.setAlreayTotalPeopleNum(hroEmployeeProjectAlreadVo.getTotalPeopleNum());
                        }
                        if (hroEmployeeProjectAlreadVo.getTotalPeopleTime() != null) {
                            item.setAlreayTotalPeopleTime(hroEmployeeProjectAlreadVo.getTotalPeopleTime());
                        }
                    }
                    //总人数
                    parm.clear();
                    parm.put("insucanceId",item.getId());
                    HroEmployeeProjectVo totalCountPeopleNum = employeeService.selectPeopleNum(parm);
                    item.setTotalCountPeopleNum(totalCountPeopleNum.getTotalPeopleNum());
//                //已实际投保的人数和人次
//                parm.clear();
//                parm.put("status","3");
//                parm.put("companyId",item.getCompanyId());
//                parm.put("property",item.getProperty());
//                parm.put("insuranceStart",item.getInsuranceStart());
//                parm.put("agreementId",item.getAgreementId());
//                HroEmployeeProjectVo hroEmployeeProjectVoByTouBao = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
//                if(hroEmployeeProjectVoByTouBao != null){
//                    if(hroEmployeeProjectVoByTouBao.getTotalPeopleNum() != null){
//                        item.setTouBaoTotalPeopleNum(hroEmployeeProjectVoByTouBao.getTotalPeopleNum());
//                    }
//                    if(hroEmployeeProjectVoByTouBao.getTotalPeopleTime() != null){
//                        item.setTouBaoTotalPeopleTime(hroEmployeeProjectVoByTouBao.getTotalPeopleTime());
//                    }
//                }
//                    //默认是没有全部投保
//                    String status = "1";
//                    //查询所有被投保通过人
//                    HroEmployee employee = new HroEmployee();
////                        employee.setInsuranceStart(item.getInsuranceStart());
////                        employee.setProperty(item.getProperty());
////                        employee.setCompanyId(item.getCompanyId());
//                    employee.setAuditStatus("1");
//                    employee.setInsucanceId(item.getId());
////                        employee.setAgreementId(item.getAgreementId());
//                    List<HroEmployee> employeeList = employeeService.selectHroEmployeeList(employee);
//                    if (CollectionUtils.isNotEmpty(employeeList)) {
//                        for (HroEmployee itemEmp : employeeList) {
//                            //根据员工id查询投保信息
//                            HroEmployeeProject queryEmployeePro = new HroEmployeeProject();
//                            queryEmployeePro.setEmployeeId(itemEmp.getId());
//                            queryEmployeePro.setStatus("0");
//                            queryEmployeePro.setAuditStatus("1");
//                            List<HroEmployeeProject> projectList = hroEmployeeProjectService.selectHroEmployeeProjectList(queryEmployeePro);
//                            if (CollectionUtils.isNotEmpty(projectList)) {
//                                status = "0";
//                            }
//                        }
//                    }
//                    item.setFlag(status);
                }
            }
            if (StringUtils.isNotBlank(hroInsucance.getEffectStatus())) {
//                if (hroInsucance.getEffectStatus().equals("2")) {
//                    //已失效
//                    for (HroInsucance item : list) {
//                        Map<String, Object> parm = new HashMap<>();
//                        //已投保人数和人次
//                        parm.clear();
//                        parm.put("auditStatus", "1");
//                        parm.put("status", "3");
//                        parm.put("companyId", item.getCompanyId());
//                        parm.put("property", item.getProperty());
//                        parm.put("insuranceStart", item.getInsuranceStart());
//                        parm.put("agreementId", item.getAgreementId());
//                        parm.put("insucanceId", item.getId());
//                        HroEmployeeProjectVo hroEmployeeProjectAlreadVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
//                        if (hroEmployeeProjectAlreadVo != null) {
//                            if (hroEmployeeProjectAlreadVo.getTotalPeopleNum() != null) {
//                                item.setAlreayTotalPeopleNum(hroEmployeeProjectAlreadVo.getTotalPeopleNum());
//                            }
//                            if (hroEmployeeProjectAlreadVo.getTotalPeopleTime() != null) {
//                                item.setAlreayTotalPeopleTime(hroEmployeeProjectAlreadVo.getTotalPeopleTime());
//                            }
//                        }
//                    }
//                }
                //失效
                for (HroInsucance item : list) {
                    Map<String, Object> parm = new HashMap<>();
                    //已投保人数和人次
                    parm.clear();
                    parm.put("auditStatus", "1");
                    parm.put("status", "3");
                    parm.put("companyId", item.getCompanyId());
                    parm.put("property", item.getProperty());
                    parm.put("insuranceStart", item.getInsuranceStart());
                    parm.put("agreementId", item.getAgreementId());
                    parm.put("insucanceId", item.getId());
                    HroEmployeeProjectVo hroEmployeeProjectAlreadVo = hroEmployeeProjectService.selectPeopleNumAndPeopleTime(parm);
                    if (hroEmployeeProjectAlreadVo != null) {
                        if (hroEmployeeProjectAlreadVo.getTotalPeopleNum() != null) {
                            item.setAlreayTotalPeopleNum(hroEmployeeProjectAlreadVo.getTotalPeopleNum());
                        }
                        if (hroEmployeeProjectAlreadVo.getTotalPeopleTime() != null) {
                            item.setAlreayTotalPeopleTime(hroEmployeeProjectAlreadVo.getTotalPeopleTime());
                        }
                    }

                }
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出保单列表
     */
    //@PreAuthorize("@ss.hasPermi('insucance:insucance:export')")
    @Log(title = "保单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @DataScope(deptAlias = "g", userAlias = "u")
    public void export(HroInsucance hroInsucance, HttpServletResponse response) {
        List<HroInsucance> list = hroInsucanceService.selectHroInsucanceList(hroInsucance);
        ExcelUtil<HroInsucance> util = new ExcelUtil<HroInsucance>(HroInsucance.class);
        util.exportExcel(response, list, "保单数据");
    }

    /**
     * 获取保单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('insucance:insucance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hroInsucanceService.selectHroInsucanceById(id));
    }

    /**
     * 新增保单
     */
    //@PreAuthorize("@ss.hasPermi('insucance:insucance:add')")
    @Log(title = "保单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HroInsucance hroInsucance) {
        return toAjax(hroInsucanceService.insertHroInsucance(hroInsucance));
    }

    /**
     * 修改保单
     */
    //@PreAuthorize("@ss.hasPermi('insucance:insucance:edit')")
    @Log(title = "保单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HroInsucance hroInsucance) {
        return toAjax(hroInsucanceService.updateHroInsucance(hroInsucance));
    }

    /**
     * 删除保单
     */
    //@PreAuthorize("@ss.hasPermi('insucance:insucance:remove')")
    @Log(title = "保单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hroInsucanceService.deleteHroInsucanceByIds(ids));
    }


    /**
     * 修改生效时间
     */
    @PostMapping("/updateStartDate")
    public AjaxResult updateStartDate(@RequestBody HroInsucance hroInsucance) {
        Long id = hroInsucance.getId();
        if (hroInsucance.getInsuranceStart() == null) {
            return AjaxResult.error("生效时间不能为空");
        }
        HroInsucance update = new HroInsucance();
        update.setId(id);
        update.setInsuranceStart(hroInsucance.getInsuranceStart());
        int i = hroInsucanceService.updateHroInsucance(update);
        if (i > 0) {
            //查询员工
            HroEmployee hroEmployee = new HroEmployee();
            hroEmployee.setInsucanceId(id);
            List<HroEmployee> employees = employeeService.selectHroEmployeeList(hroEmployee);
            if (CollectionUtils.isNotEmpty(employees)) {
                List<HroEmployee> updateList = new ArrayList<>();
                for (HroEmployee item : employees) {
                    HroEmployee updateEmployee = new HroEmployee();
                    updateEmployee.setId(item.getId());
                    updateEmployee.setInsuranceStart(hroInsucance.getInsuranceStart());
                    updateList.add(updateEmployee);
                }
                employeeService.updateHroEmployeeBatch(updateList);
            }
        }
        return toAjax(i);
    }

    /**
     * 投保导出  导出人员去投保
     *
     * @param response
     * @param hroInsucance
     */
    // @PreAuthorize("@ss.hasPermi('insucance:insucance:insureExport')")
    @Log(title = "投保人员与保险方案中间", businessType = BusinessType.EXPORT)
    @PostMapping("/insureExport")
    public void insureExport(HttpServletResponse response, HroInsucance hroInsucance) {
        Long[] ids = new Long[0];
        if (hroInsucance != null) {
            ids = hroInsucance.getIds();
        }
        //数据组
        List<List> dataList = new ArrayList<>();
        //名字组
        List<String> sheetNames = new ArrayList<>();
        //对象
        List<Class> classList = new ArrayList<>();
        //查询出所有审核通过且待投保的员工与方案
        List<HroEmployeeProjectExportVo> list;
        if (ids != null && ids.length > 0) {
            list = hroEmployeeProjectService.selectInsureExport(ids);
        } else {
            list = hroEmployeeProjectService.selectInsureExport(null);
        }
        if (list.isEmpty()){
            return;
        }
        //筛选出不同的供应商存于Set
        Set<Long> collect = list.stream().map(HroEmployeeProjectExportVo::getSupplierId).collect(Collectors.toSet());
        //初始化map，供应商id:对应的list
        HashMap<Long, List<HroEmployeeProjectExportVo>> longListHashMap = new HashMap<>();
        for (Long supId : collect) {
            List<HroEmployeeProjectExportVo> hroEmployeeProjectExportVos = new ArrayList<>();
            longListHashMap.put(supId, hroEmployeeProjectExportVos);
        }
        //填充map
        for (HroEmployeeProjectExportVo vo : list) {
            Long supplierId = vo.getSupplierId();
            for (Long id : collect) {
                if (supplierId.equals(id)) {
                    List<HroEmployeeProjectExportVo> hroEmployeeProjectExportVos = longListHashMap.get(id);
                    hroEmployeeProjectExportVos.add(vo);
                }
            }
        }
        //供应商名称
        String supplierName = "";
        //主体公司名称
        String signMainName = "";
        //生成时间
        String insuranceStartStr = "";
        //遍历map，填充以上三种list
        Set<Map.Entry<Long, List<HroEmployeeProjectExportVo>>> entries = longListHashMap.entrySet();
        for (Map.Entry<Long, List<HroEmployeeProjectExportVo>> entry : entries) {
            List<HroEmployeeProjectExportVo> value = entry.getValue();
            for (int i = 0; i < value.size(); i++) {
                HroEmployeeProjectExportVo hroEmployeeProjectExportVo = value.get(i);
                hroEmployeeProjectExportVo.setSerialNumber(i + 1);
            }
            String supplierNameSelect = value.get(0).getSupplierName();
            if (supplierNameSelect != null) {
                supplierName = supplierNameSelect;
            }
            Date insuranceStart = value.get(0).getInsuranceStart();
            if (insuranceStart != null) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                insuranceStartStr = simpleDateFormat.format(insuranceStart);
            }
            String signMainNameSelect = value.get(0).getSignMainName();
            if (signMainNameSelect != null) {
                signMainName = signMainNameSelect;
            }
            dataList.add(value);
            sheetNames.add(signMainName + "-" + supplierName + "-" + insuranceStartStr);
            classList.add(HroEmployeeProjectExportVo.class);
        }
        ExcelUtil<HroEmployeeProjectExportVo> util = new ExcelUtil(classList);
        //util.exportExcel(dataList, sheetNames, "投保数据");
        util.exportExcel(response, dataList, sheetNames,null);

//        //投保状态改为 投保中
//        if (!list.isEmpty()){
//            for (HroEmployeeProject next : list) {
//                next.setStatus(HroEnum.INSURE_EMP_INSURE_STATUS_1.getCode());
//                hroEmployeeProjectService.updateHroEmployeeProject(next);
//            }
//        }

    }

    /**
     * 投保导入
     *
     * @param file 附件
     * @return
     * @throws Exception
     */
    //@PreAuthorize("@ss.hasPermi('insucance:insucance:insureImport')")
    @Log(title = "投保人员与保险方案中间", businessType = BusinessType.IMPORT)
    @PostMapping("/importInsureResult")
    public AjaxResult importInsureResult(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("参数错误！请联系管理员");
        }
        String message = "";
        try {
            //获取Excel数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startStr = sdf.format(new Date());
//            System.err.println("开始时间===="+startStr);
            ExcelUtil<HroEmployeeProjectExportVo> util = new ExcelUtil<HroEmployeeProjectExportVo>(HroEmployeeProjectExportVo.class);
            String endStr = sdf.format(new Date());
            // 获取到的人员数据
            List<HroEmployeeProjectExportVo> vos = util.importExcelLotSheet(file.getInputStream(), 0);
//            System.err.println("结束时间===="+endStr);
            String operName = getUsername();
            message = hroEmployeeProjectService.importInsureResult(vos, operName);
        } catch (NullPointerException e) {
            message = "导入失败！请检查文件数据格式是否符合模板！";
            e.printStackTrace();
        } catch (ServiceException e) {
            message = e.getMessage();
            e.printStackTrace();
        } catch (Exception e) {
            message = "导入失败！请联系管理员！";
            e.printStackTrace();
        }
        return success(message);
    }

    /**
     * 下载模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<HroEmployeeProjectExportVo> util = new ExcelUtil<HroEmployeeProjectExportVo>(HroEmployeeProjectExportVo.class);
        util.importTemplateExcel(response, "投保导入模板");
    }


    /**
     * 生成付款单
     */
    @GetMapping("/makePaymentForm/{ids}")
    public AjaxResult makePaymentForm(@PathVariable Long[] ids) {
        //查询没有参与过生成付款单的投保方案，按主体公司和供应商进行分组
        List<MakePaymentOrderVo> makePaymentOrderVos = employeeProjectService.makePaymentOrderGroupBySupplierIdAndSignMainId(ids);
        if (CollectionUtils.isNotEmpty(makePaymentOrderVos)) {
            for (MakePaymentOrderVo item : makePaymentOrderVos) {
                //向付款单主表进行插入
                HroPaymentForm paymentForm = new HroPaymentForm();
                paymentForm.setSignMainId(item.getSignMainId());
                paymentForm.setSupplierId(item.getSupplierId());
                paymentForm.setTotalPeopleNum(item.getTotalPeopleNum());
                paymentForm.setTotalPeopleTime(item.getTotalPeopleTime());
                paymentForm.setPayAmount(item.getTotalPrice());
                paymentForm.setInvoicingStatus(String.valueOf(HroEnum.INVOICING_NOT.getCode()));
                paymentForm.setPayingStatus(String.valueOf(HroEnum.PAY_NOT.getCode()));
                paymentForm.setType("1");
                int i = paymentFormService.insertHroPaymentForm(paymentForm);
                if (i > 0) {
                    // 根据返回的供应商id和主体公司id，查询hro_employee_project id
                    MakePaymentOrderVo makePaymentOrderVo = new MakePaymentOrderVo();
                    makePaymentOrderVo.setSignMainId(item.getSignMainId());
                    makePaymentOrderVo.setSupplierId(item.getSupplierId());
                    makePaymentOrderVo.setInsucanceId(item.getInsucanceId());
                    List<MakePaymentOrderVo> orderVoList = employeeProjectService.makePaymentOrderBySupplierIdAndSignMainId(makePaymentOrderVo);
                    if (CollectionUtils.isNotEmpty(orderVoList)) {
                        for (MakePaymentOrderVo vo : orderVoList) {
                            //插入详情
                            HroPaymentFormDetail paymentFormDetail = new HroPaymentFormDetail();
                            paymentFormDetail.setPaymentFormId(paymentForm.getId());
                            paymentFormDetail.setCompanyId(vo.getCompanyId());
                            paymentFormDetail.setEmployeeProjectId(vo.getEmployeeProjectId());
                            int j = paymentFormDetailService.insertHroPaymentFormDetail(paymentFormDetail);
                            if (j > 0) {
                                //修改emploee_project表的参与付款的状态
                                HroEmployeeProject updateEmployeeProject = new HroEmployeeProject();
                                updateEmployeeProject.setId(vo.getEmployeeProjectId());
                                updateEmployeeProject.setPaymentStatus("1");
                                employeeProjectService.updateHroEmployeeProject(updateEmployeeProject);

                            }
                        }
                    }
                }
            }
            for (MakePaymentOrderVo item : makePaymentOrderVos) {
                //更新保单表付款单状态
                HroInsucance updateInsucance = new HroInsucance();
                updateInsucance.setId(item.getInsucanceId());
                updateInsucance.setPaymentStatus("1");
                hroInsucanceService.updateHroInsucance(updateInsucance);
            }
        }
        Map<String, Object> res = new HashMap<>();
        res.put("res", makePaymentOrderVos.size() > 0 ? "生成成功" : "生成失败");
        res.put("data", makePaymentOrderVos.size() > 0 ? "1" : "0");
        return AjaxResult.success(res);
    }


    //查询是否有没经过算法的人
    @GetMapping("/getNotDecisionByInsucanceId/{insuranceId}")
    public AjaxResult getNotDecisionByInsucanceId(@PathVariable("insuranceId") Long insuranceId) {
        HroEmployee selctEmp = new HroEmployee();
        selctEmp.setInsucanceId(insuranceId);
        selctEmp.setDecisionOpenStatus("1");
        selctEmp.setIsDecision("0");
        List<HroEmployee> employees = employeeService.selectHroEmployeeList(selctEmp);
        if (CollectionUtils.isNotEmpty(employees)) {
            return AjaxResult.success(false);
        } else {
            return AjaxResult.success(true);
        }
    }


}
